/**
 * 通用vue模版
 * */
import {
  mapMutations,
  mapState
} from 'vuex'
const rem = 100
export default {
  computed: mapState({
    user: 'user'
  }),
  methods: {
    BIND_USER: mapMutations(['BIND_USER']).BIND_USER,
    BIND_LOADING: mapMutations(['BIND_LOADING']).BIND_LOADING,
    px2rem (px) {
      return `${px / rem}rem`
    },
    px (px) {
      return `${px}px`
    },
    /**
     * 格式化电话号码
     *
     * @param {any} phone:完整的手机号码 如:13712345678
     * @param {int} args: 可不传，默认为 0
     *  args = 0 返回的格式:137****5678
     *  args = 1 返回的格式:**5678
     * @returns {String} 格式化后的手机号码
     */
    formatPhone: function (phone, args = 0) {
      let tel = phone + ''
      let reg = 0
      // 根据参数设置手机格式
      if (args === 0) {
        reg = /^(\d{3})\d{4}(\d{4})$/
        if (tel) {
          tel = tel.replace(reg, '$1****$2')
        }
        reg = /^(\d{3})\d{3}(\d{4})$/
        if (tel) {
          tel = tel.replace(reg, '$1***$2')
        }
      } else if (args === 1) {
        reg = /^\d{7}(\d{4})$/
        if (tel) {
          tel = tel.replace(reg, '**$1')
        }
      }

      return tel
    },
    setLocalImg (val, imgtype = 'png') {
      let context = require.context('@image/', true)
      // context.keys() //打印所有匹配的列表
      // 找到匹配的图片
      let empty = context(`./${val}.${imgtype}`)
      console.error(empty)
      return empty
    }
    // 解析网页数据
    // pddetailSySrc (pddetail) {
    //   return pddetail
    //     .replace(/src="([^"]+)"/g, '')
    //     .replace(/title="([^"]+)"/g, function (a, b) {
    //       return 'src="' + b + '"'
    //     })
    // }
  },
  data () {
    return {
      loadHide: true
    }
  },
  mounted () { this.BIND_LOADING(false) },
  beforeUpdate () {
    if (this.loadHide === true) {
      this.$nextTick(() => {
        this.BIND_LOADING(false)
      })
    }
  },
  activated () {
    if (this.loadHide === true) {
      this.$nextTick(() => {
        this.BIND_LOADING(false)
      })
    }
  }
}
